package com.gnerv.sylvanas.business.manage.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.business.manage.logic.PostLogicService;
import com.gnerv.sylvanas.business.manage.model.dto.PostCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.PostDTO;
import com.gnerv.sylvanas.business.manage.model.dto.PostUpdateDTO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/manage/post")
public class PostController {

    private final PostLogicService postLogicService;

    @PostMapping(value = "")
    public RestResult<Void> create(@RequestBody PostCreateDTO postCreateDTO) {
        return postLogicService.create(postCreateDTO);
    }

    @DeleteMapping(value = "")
    public RestResult<Void> deleteByUkId(@RequestParam(value = "ukId") String ukId) {
        return postLogicService.deleteByUkId(ukId);
    }

    @PutMapping(value = "")
    public RestResult<Void> updateByUkId(@RequestBody PostUpdateDTO postUpdateDTO) {
        return postLogicService.updateByUkId(postUpdateDTO);
    }

    @GetMapping(value = "")
    public RestResult<PostDTO> selectByUkId(@RequestParam(value = "ukId") String ukId) {
        return postLogicService.selectByUkId(ukId);
    }

    @GetMapping(value = "/name")
    public RestResult<PostDTO> selectByName(@RequestParam(value = "name") String name) {
        return postLogicService.selectByName(name);
    }

    @GetMapping(value = "/list")
    public RestResult<List<PostDTO>> selectListByName(@RequestParam(required = false, value = "name") String name) {
        return postLogicService.selectListByName(name);
    }

}
